﻿using System;
using System.Linq;
using Hjt212Parser;
using log4net;
using TcpServer.Facade;
using TcpServer.Orm;

namespace TcpServer.DataProcessor
{
    class Platform2011Processor : IProcessor
    {
        private readonly ILog log = LogManager.GetLogger(typeof(Platform2011Processor));

        public void Persist()
        {
            throw new NotImplementedException();
        }

        public void Persist(DustEntities context, SocketClientInfo content)
        {
            if (string.IsNullOrWhiteSpace(content.MN)) return;
            try
            {
                //有效字符串
                if (Parser.IsLengthRight(content.ReceivedMsg))
                {
                    //根据MN号获取基本信息
                    var equipment = BaseInfoBufferAndHelper.GetBaseWholeInfoByMn(content.MN);
                    //有效企业信息
                    if (equipment != null)
                    {
                        var monitor =
                            context.RealDataMonitors.FirstOrDefault(
                                t => t.StatCodeID == equipment.StatCodeID && t.DevCodeID == equipment.DevCodeID);
                        if (monitor == null)
                        {
                            monitor = new RealDataMonitor
                            {
                                StatCodeID = equipment.StatCodeID,
                                DevCodeID = equipment.DevCodeID
                            };
                            context.RealDataMonitors.Add(monitor);
                        }
                        var temp = Parser.GetStringByKey("a34001-Flag", content.ReceivedMsg);
                        //if (!temp.Equals("N")) return;
                        if (temp.Equals("N"))
                        {
                            monitor.TP = Parser.GetDoubleByKey("a34001-Rtd", content.ReceivedMsg);
                        }
                        temp = Parser.GetStringByKey("a50001-Flag", content.ReceivedMsg);
                        //if (!temp.Equals("N")) return;
                        if (temp.Equals("N"))
                        {
                            monitor.Db = Parser.GetDoubleByKey("a50001-Rtd", content.ReceivedMsg);
                        }
                        monitor.DataTime = Parser.GetDateTimeByKey("DataTime", content.ReceivedMsg);
                        temp = Parser.GetStringByKey("a01008-Flag", content.ReceivedMsg);
                        //if (!temp.Equals("N")) return;
                        if (temp.Equals("N"))
                        {
                            monitor.WindDirection = Parser.GetDoubleByKey("a01008-Rtd", content.ReceivedMsg);
                        }
                        temp = Parser.GetStringByKey("a01007-Flag", content.ReceivedMsg);
                        //if (!temp.Equals("N")) return;
                        if (temp.Equals("N"))
                        {
                            monitor.WindSpeed = Parser.GetDoubleByKey("a01007-Rtd", content.ReceivedMsg);
                        }
                        temp = Parser.GetStringByKey("a01001-Flag", content.ReceivedMsg);
                       // if (!temp.Equals("N")) return;
                        if (temp.Equals("N"))
                        {
                            monitor.Temperature = Parser.GetDoubleByKey("a01001-Rtd", content.ReceivedMsg);
                        }
                        temp = Parser.GetStringByKey("a01002-Flag", content.ReceivedMsg);
                       // if (!temp.Equals("N")) return;
                        if (temp.Equals("N"))
                        {
                            monitor.Humidity = Parser.GetDoubleByKey("a01002-Rtd", content.ReceivedMsg);
                        }
                        temp = Parser.GetStringByKey("a01006-Flag", content.ReceivedMsg);
                        //if (!temp.Equals("N")) return;
                        if (temp.Equals("N"))
                        {
                            monitor.AirPressure = Parser.GetDoubleByKey("a01006-Rtd", content.ReceivedMsg);
                        }
                        context.SaveChanges();
                        //从字符串获取污染物代码s
                        var pollutantCodes = Parser.GetPollutantCodes(content.ReceivedMsg);
                        if (monitor.DataTime.HasValue)
                        {
                            foreach (var code in pollutantCodes)
                            {
                                var factor = BaseInfoBufferAndHelper.GetFactorByCode(code);
                                var rtd = Parser.GetDoubleByKey(code + "-Rtd", content.ReceivedMsg);
                                var flag = Parser.GetStringByKey(code + "-Flag", content.ReceivedMsg);                              
                                if (factor == null || !rtd.HasValue || !flag.Equals("N")) continue;
                                var rc = new RealDataInforRC
                                {
                                    StatCodeID = equipment.StatCodeID,
                                    DevCodeID = equipment.DevCodeID,
                                    MonitorTime = monitor.DataTime.Value,
                                    MonitorFactorID = factor.ID,
                                    Rtd = rtd.Value
                                };
                                context.RealDataInforRCs.Add(rc);
                                context.SaveChanges();
                            }

                            var main = new RealDataInforRCMain
                            {
                                StatCodeID = equipment.StatCodeID,
                                DevCodeID = equipment.DevCodeID,
                                MonitorTime = monitor.DataTime.Value
                            };

                            foreach (var code in pollutantCodes)
                            {
                                var factor = BaseInfoBufferAndHelper.GetFactorByCode(code);
                                var rtd = Parser.GetDoubleByKey(code + "-Rtd", content.ReceivedMsg);
                                var flag = Parser.GetStringByKey(code + "-Flag", content.ReceivedMsg);
                                if (factor == null || !rtd.HasValue || !flag.Equals("N")) continue;
                                var detail = new RealDataInforRCDetail
                                {
                                    MonitorFactorID = factor.ID,
                                    Rtd = rtd.Value
                                };
                                main.RealDataInforRCDetails.Add(detail);
                            }
                            context.RealDataInforRCMains.Add(main);
                            context.SaveChanges();
                        }
                    }
                    else
                    {
                        log.Warn(content.MN + " 没有对应基础信息！");
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error( ex);
                log.Error( content.ReceivedMsg);

            }
        }
        public void Persist(DataCommunicationEntities context, SocketClientInfo content)
        {
            throw new NotImplementedException();
        }
    }
}
